package gui.pannelPartie.MatchList;

import gui.BeginDialogue.ChargementException;
import gui.Error.ErrorDialogue;
import gui.pannelPartie.PanelPartie;
import html.HtmlBody;
import html.HtmlPageSaveButton;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import core.Match;
import core.Team;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

public class PanelMatchList extends JPanel implements ActionListener , HtmlBody , Printable{

	private static final long serialVersionUID = 8795531932619057724L;
    private List<MatchInfo> mi;
	private EndGame ss;
	private Frame onError;
    private EndManche endManche;
    private static String matchInfoSaveSeparator ="&"; 

    
	public PanelMatchList(List<Match> matchs,Frame owner,EndManche em){
		onError=owner;
		endManche=em;
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		mi = new ArrayList<MatchInfo>();
		for(Match m:matchs){
			MatchInfo aux = new MatchInfo(m,owner);
			mi.add(aux);
			add(aux);
		}
		addForgroundBar(owner);
	}


	public PanelMatchList(String charge, Frame owner, PanelPartie em,Team[] teams,int inNbPoints) throws ChargementException {
		onError=owner;
		endManche=em;
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		mi = new ArrayList<MatchInfo>();
		Scanner sc =new Scanner(charge);
		sc.useDelimiter(matchInfoSaveSeparator);
		boolean fini=false;
		if(sc.hasNextBoolean())
			fini=sc.nextBoolean();
		else throw new ChargementException("Erreur de chargement");
		while(sc.hasNext()){
			MatchInfo aux = new MatchInfo(owner,sc.next(),teams,inNbPoints);
			mi.add(aux);
			add(aux);
		}		
		addForgroundBar(owner);
		ss.setEnabled(fini);
	}

	private void addForgroundBar(Frame owner){
		ss=new EndGame(this);
		JPanel aux= new JPanel();
		aux.setLayout(new BoxLayout(aux,BoxLayout.X_AXIS));
		aux.setLayout(new FlowLayout());
		aux.add(new HtmlPageSaveButton(this,"","Enregistrer",owner));
		aux.add(new PrintButton(this));
		aux.add(ss);
		add(aux);
	}

	public void actionPerformed(ActionEvent arg0) {
		try {
			for(MatchInfo m:mi)
				if(m.isVisible())
					m.submitScore();
			ss.setEnabled(false);
			endManche.endManche();
		} catch (Exception e) {
			new ErrorDialogue(onError,e.getMessage());
		}		
	}
	
	public void refresh(List<Match> matchs){
		int i=0;
		ss.setEnabled(true);
		for(;i<matchs.size();i++){
			mi.get(i).refresh(matchs.get(i));
			mi.get(i).setVisible(true);
		}
		for(;i<mi.size();i++){
			mi.get(i).setVisible(false);
		}
	}



	public String getHtmlBody() {
		StringBuffer res= new StringBuffer();
		res.append("<table>\n");
		for (MatchInfo m:mi)
			if (m.isVisible())
				res.append(m.getHtmlBody());
		res.append("</table>\n");
		return res.toString();
	}


	@Override
	public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)throws PrinterException {	
			if(pageIndex>0)
				return NO_SUCH_PAGE;
			printAll(graphics);
			graphics.dispose();
			return PAGE_EXISTS;
	}


	public String save() {
		StringBuffer res = new StringBuffer();  
		res.append(ss.isEnabled()+matchInfoSaveSeparator);
		for(int i = 0 ;i< mi.size();i++){
			res.append(mi.get(i).save());
			if(i!=mi.size()-1)
				res.append(matchInfoSaveSeparator);
		}
		return res.toString();
	}
}
